package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"online/api/requset"
	"online/intrenal/dao"
	"online/intrenal/model"
)

func AddComment(c *gin.Context) {
	var req requset.AddCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	result := dao.Create(&model.Comment{
		UserId:    c.GetUint("userId"),
		OnlineId:  req.OnlineId,
		CollectId: req.CollectId,
		Content:   req.Content,
	})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "评论失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "评论成功",
		"date": true,
	})
}

func DelComment(c *gin.Context) {
	var req requset.DelCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	result := dao.Delete(&model.Comment{Id: req.Id})
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "评论删除失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "评论删除成功",
		"date": true,
	})
}

func ListComment(c *gin.Context) {
	var req requset.ListCommentReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": nil,
		})
		return
	}
	var comment []model.Comment
	result := dao.GetListByFields(&model.Comment{
		Id:      req.Id,
		Content: req.Content,
	}, &comment)
	if !result {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "评论展示失败",
			"date": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "评论展示成功",
		"date": comment,
	})
}
